﻿using DevProtocolApi.Dtos.system;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Service.IService.ISysService;

namespace DevProtocolApi.Controllers.sysControllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DeptGroupController : BaseController
    {
        private readonly IDeptGroupService _deptGroupService;
        private readonly ILogger<DeptGroupController> _logger;

        public DeptGroupController(
            IDeptGroupService deptGroupService,
            ILogger<DeptGroupController> logger)
        {
            _deptGroupService = deptGroupService;
            _logger = logger;
        }

        /// <summary>
        /// 添加组别到部门
        /// </summary>
        /// <param name="deptId">部门ID</param>
        /// <param name="groupIds">组别ID列表</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<IActionResult> AddGroups(string deptId, [FromBody] List<string> groupIds)
        {
            try
            {
                bool success =  _deptGroupService.AddGroupsToDept(deptId, groupIds);
                return success ? Success("添加组别成功") : Failed("添加组别失败");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"添加部门组别失败: {deptId}");
                return Failed($"添加失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 从部门移除组别
        /// </summary>
        /// <param name="deptId">部门ID</param>
        /// <param name="groupIds">组别ID列表</param>
        /// <returns>操作结果</returns>
        [HttpDelete]
        public async Task<IActionResult> RemoveGroups(string deptId, [FromBody] List<string> groupIds)
        {
            try
            {
                bool success =  _deptGroupService.RemoveGroupsFromDept(deptId, groupIds);
                return success ? Success("移除组别成功") : Failed("移除组别失败");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"移除部门组别失败: {deptId}");
                return Failed($"移除失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 更新部门-组别关联状态
        /// </summary>
        /// <param name="relationId">关联ID</param>
        /// <param name="dto">状态更新DTO</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<IActionResult> UpdateStatus(string relationId, [FromBody] ToggleStatusDto dto)
        {
            try
            {
                bool success =  _deptGroupService.UpdateDeptGroupStatus(relationId, dto.IsActive);
                return success ? Success("状态更新成功") : Failed("状态更新失败");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"更新部门组别状态失败: {relationId}");
                return Failed($"更新失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 获取部门关联的组别
        /// </summary>
        /// <param name="deptId">部门ID</param>
        /// <returns>部门关联的组别列表</returns>
        [HttpGet]
        public async Task<IActionResult> GetByDept(string deptId)
        {
            try
            {
                var deptGroups =  _deptGroupService.GetDeptGroups(deptId);
                return Success(deptGroups);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"获取部门组别失败: {deptId}");
                return Failed($"获取失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 获取部门可用的组别
        /// </summary>
        /// <param name="deptId">部门ID</param>
        /// <returns>部门可用的组别列表</returns>
        [HttpGet]
        public async Task<IActionResult> GetAvailableGroups(string deptId)
        {
            try
            {
                var groups =  _deptGroupService.GetAvailableGroupsForDept(deptId);
                return Success(groups);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"获取可用组别失败: {deptId}");
                return Failed($"获取失败: {ex.Message}");
            }
        }
    }
}
